package com.beverage.lemonade.UI;

import com.beverage.lemonade.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class SlideListView extends ListView {
	protected ListAdapter adapter;
	protected Context context;
	protected BaseActivity baseActivity;
	protected BaseFragment baseFragment;
	protected Drawable divider;

	public SlideListView(Context context, BaseFragment base) {
		super(context);
		this.context = context;
		this.divider = context.getResources().getDrawable(
				R.drawable.general_list_divider);
		this.baseFragment = base;
		this.adapter = new ListAdapter(context);
		setDivider(divider);
		setOnItemClickListener(new ClickEvent());
	}

	public SlideListView(Context context, BaseActivity base) {
		super(context);
		this.context = context;
		this.divider = context.getResources().getDrawable(
				R.drawable.general_list_divider);
		this.baseActivity = base;
		this.adapter = new ListAdapter(context);
		setDivider(divider);
		setOnItemClickListener(new ClickEventForActivity());

	}

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.list, null);
	}

	public void setAllItem() {
		setAdapter(adapter);
	}

	public void addOneItem(Intent intent, String Tag, int iconRes) {
		adapter.add(new Item(intent, Tag, iconRes));
	}

	public void addOneItem(Intent intent, String Tag) {
		adapter.add(new Item(intent, Tag));
	}

	private class Item {
		public String tag;
		public int iconRes = 0;
		public Intent intent;

		public Item(Intent intent, String tag, int iconRes) {
			this.tag = tag;
			this.iconRes = iconRes;
			this.intent = intent;
		}

		public Item(Intent intent, String tag) {
			this.tag = tag;
			this.intent = intent;
		}

		public Intent getIntent() {
			return intent;
		}
	}

	public class ListAdapter extends ArrayAdapter<Item> {

		public ListAdapter(Context context) {
			super(context, 0);
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(getContext()).inflate(
						R.layout.row, null);
			}

			if (getItem(position).iconRes != 0) {
				ImageView icon = (ImageView) convertView
						.findViewById(R.id.row_icon);
				icon.setImageResource(getItem(position).iconRes);
			}

			TextView title = (TextView) convertView
					.findViewById(R.id.row_title);
			title.setText(getItem(position).tag);
			title.setTextColor(0xFFFFFFCC);

			convertView.setBackgroundResource(R.drawable.general_list_back);

			return convertView;
		}
	}

	class ClickEvent implements OnItemClickListener {

		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3) {
			// TODO Auto-generated method stub
			System.out.println(position);
			Item item = adapter.getItem(position);
			baseFragment.toggle();
			baseFragment.startActivity(item.getIntent());
			baseFragment.overridePendingTransition(0, 0);
			baseFragment.finish();
		}
	}

	class ClickEventForActivity implements OnItemClickListener {

		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3) {
			// TODO Auto-generated method stub
			System.out.println(position);
			Item item = adapter.getItem(position);
			baseActivity.toggle();
			baseActivity.startActivity(item.getIntent());
			baseActivity.overridePendingTransition(0, 0);
			baseActivity.finish();

		}
	}
}
